{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BOOL-001\n",
    "\n",
    "> bool 有值有几个？\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "bool 值只有 2 个：True 和 False。注意是大小写要一致。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BOOL-002\n",
    "\n",
    "> 把整数转 bool\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bool(0), bool(1), bool(-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除 0 转 False 外，其他全部转 True。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BOOL-003\n",
    "\n",
    "> 把字符串转 bool\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bool(\"\"), bool(\" \")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "除空字符串(长度为 0 的字符串)转为 False, 其他都为 True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BOOL-004\n",
    "\n",
    "> `bool(\"False\")`的结果是什么？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bool(\"False\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BOOL-005\n",
    "\n",
    "> 把列表转 bool\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bool([]), bool([0]), bool([False])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "空列表 -> False; 其他 -> True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BOOL-006\n",
    "\n",
    "> 字典转 bool\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "bool({}), bool({\"x\": False})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "空字典 -> False, 其他 -> True\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BOOL-007\n",
    "\n",
    "> 自定义类型转 bool\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Car:\n",
    "    def __init__(self, name, can_use=True):\n",
    "        self.name = name\n",
    "        self.can_use = can_use\n",
    "\n",
    "    def __bool__(self):\n",
    "        return self.can_use\n",
    "\n",
    "\n",
    "bool(Car(\"红旗\")), bool(Car(\"宝马\", True)), bool(Car(\"奔驰\", False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用`__bool__`魔术方法进行 bool 转换\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BOOL-008\n",
    "\n",
    "> bool 转 str\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str(True), str(False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## BOOL-009\n",
    "\n",
    "> 把'true'(不区分大小写)转为 True, 其他都为 False。非字符串抛出异常。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def is_true(value) -> bool:\n",
    "    if not isinstance(value, str):\n",
    "        raise TypeError(\"参数value必须为字符串类型\")\n",
    "    if value.lower() == \"true\":\n",
    "        return True\n",
    "    return False\n",
    "\n",
    "\n",
    "is_true(\"true\"), is_true(\"True\"), is_true(\"false\"), is_true(\"1\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
